package bod.engine.map.blocks;

import java.awt.image.BufferedImage;

import bod.engine.Engine;
import bod.engine.map.Map;

public class Block {

	public static final int	size	= 16;
	private BlockType		type;
	private Back			back;
	private int				x;
	private int				y;
	private byte			img;
	private byte			delay;

	public Block(BlockType type, int x, int y) {
		this(type, type.defaultBack(), x, y);
	}

	public Block(BlockType type, Back back, int x, int y) {
		this.type = type;
		this.back = back;
		this.x = x;
		this.y = y;
		this.img = 0;
		this.delay = 0;
	}

	public void tick() {
		if (this.type.isLiquid()) {
			if (this.delay < 20) {
				this.delay++;
			} else {
				this.delay = 0;
				Block bt = Engine.getBlock(this.x, this.y + size);
				if (bt != null && bt.getType() == Map.air) {
					Engine.setBlock(new Block(Map.air, this.back, this.x,
							this.y), this.x, this.y);
					Engine.setBlock(new Block(this.type, this.back, this.x,
							this.y + size), this.x, this.y + size);
				}
			}
		}
	}

	public void update(int dt, int dl, int db, int dr) {
		this.img = this.type.getImageByBorder(dt, dl, db, dr);
	}

	public byte getDensity() {
		return this.type.getDensity();
	}

	public boolean isSolid() {
		return this.type.isSolid();
	}

	public boolean isLiquid() {
		return this.type.isLiquid();
	}

	public BlockType getType() {
		return this.type;
	}

	public BufferedImage getImage() {
		return this.type.getImage(this.img);
	}

	public BufferedImage getBack() {
		return this.back.getImage();
	}

	public int getX() {
		return this.x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return this.y;
	}

	public void setY(int y) {
		this.y = y;
	}

}